Closure in Scheme

Introduces how closure works in Scheme programming language.

Table of Contents

1. Environment and Frames

In Scheme runtime, environment records the mapping between variable names and values. An environment consists of a chain of frames where:

  • each frame is a table containing bindings from variable names to values
  • each frame contains a pointer to its outer environment
  • the outer-most is the global environment, which stores variables declared with define and built-in functions like +, car.

When Scheme wants to find a variable, it starts from the current frame, all the way the global environment along the pointer chain, until found or reports an error if no found.

2. Understanding Closure in Scheme

To understand closures’ behaviours, we have to investigate what happens at the time of its creation and its invocation.

2.1. Creation

When the closure is created through lambda expressions, Scheme does not execute the function body. Instead, it generates a procedure object that contains:

  • Code. Argument list and function body
  • Environment pointer that points to the current environment at the time of definition

So closure is just code + environment.

2.2. Invocation

Upon invocation, Scheme

  1. creates a new frame
  2. make the env pointer of this new frame point to the saved environment
  3. execute the function body in the new environment constructed by this new frame

Date: 2026-09-30 Wed

Author: ArcaLunar